<?php
/**
 * 区域管理
 */
include (dirname(__FILE__)."/session.inc.php");//全局变量

//页面属于
$mod=trim(empty($_REQUEST['mod'])?"list":$_REQUEST['mod']);
$smarty->assign("mod",$mod);
$link_url="";
$tb_area="`".$tbprefix."area`";
$pagesize =15;
$page =empty($_REQUEST['page'])||($_REQUEST['page']<0)?"1":trim($_REQUEST['page']);

switch ($mod){
		//区域编辑
	case "edit":

		//页面动作
		@$act=isset($_REQUEST['act'])?trim($_REQUEST['act']):"";
		@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		/**
		 * 获取地址栏传递的id
		 */
		$pid=empty($_GET['pid'])?"0":$_GET['pid'];
		$level=empty($_GET['level'])?"1":$_GET['level'];
		$smarty->assign("pid",$pid);
		$smarty->assign("level",$level);


		switch ($act){
			//添加
			case "add":

				$fid_logo=$db->get_one("SELECT `title` FROM ".$tb_area." WHERE  `infoid`='".$pid."' AND 1");

				$fid_back=$pid;//用于返回id

				$titles_edit="添加";

				$oneinfo=array("infoid"=>0);

				break;

			//更新
			case "update":

				$fid=$db->get_one("SELECT `parentid`,`title` FROM ".$tb_area." WHERE  `infoid`='".$pid."' AND 1");
				$fid_back=$fid['parentid'];

				$fid_logo=$db->get_one("SELECT `title` FROM ".$tb_area." WHERE  `infoid`='".$fid_back."' AND 1");
				$titles_edit="修改";

				$oneinfo=$db->get_one("SELECT `infoid`, `parentid`, `level`, `orderid`, `title`,`ishow`
							FROM ".$tb_area."
							WHERE  `infoid`='".$pid."' AND 1");

				break;

			case "change":

				//用于获取hid动作
				$hid=isset($_POST['actId'])?$_POST['actId']:"";
				
				//名称
				$areaName=isset($_POST['classname'])?$fun->checkInput(trim($_POST['classname'])):"";
				if (!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$areaName))
				{
					$fun->msg('请您输入中文',"",1);
				}
				$dataArray['title']=$areaName;
				//显示
				$dataArray['ishow']=!empty($_POST['number'])?trim($_POST['number']):0;
				$dataArray['is_valid']=1;
				//排序
				$dataArray['orderid']=empty($_POST['orderid'])?"0":$_POST['orderid'];


				if(empty($hid)){

					$dataArray['parentid']=((empty($pid))?"0":$pid);
					$dataArray['level']=$level;

					if($db->insert($tb_area,$dataArray))
					{
						$fun->msg('添加成功','?mod=edit&act=add&pid='.$pid.'&level='.$level.'&page='.$page,1);
						$db->close();
					}
					else
					{
						$fun->msg('添加失败','?mod=edit&act=add&pid='.$pid.'&level='.$level.'&page='.$page,1);
					}
				}else{

					if($db->update($tb_area,$dataArray,$condition="`infoid`='".$hid."' AND 1"))
					{
						$fun->msg('更新成功','?mod=edit&act=update&pid='.$hid.'&level='.$level.'&page='.$page,1);
						$db->close();
					}
					else
					{
						$fun->msg('更新失败','?mod=edit&act=update&pid='.$hid.'&level='.$level.'&page='.$page,1);
					}
				}

				break;

			default:
				$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
				break;
		}

		$areaTitle=(empty($fid_logo['title']))?"省市":$fid_logo['title'];
		$smarty->assign("areaTitle",$areaTitle);//标题

		$smarty->assign("one",$oneinfo);//要修改的区域
		$smarty->assign("titleAct",$titles_edit);//页面操作动作标题
		$smarty->assign("Returnfpage",$back_url);//返回列表

		break;

	//是否显示
	case "ishow":
		/*更新是否显示*/
		@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		if($_GET['ishowId']!=""){

			$ishow=$_GET['ishowId'];
			$infoid=empty($_GET['infoid'])?0:$_GET['infoid'];

			$totalInfo=$db->get_one("SELECT COUNT(`infoid`) AS total  FROM ".$tb_area." WHERE  `infoid`='".$infoid."' AND 1");
			$total=$totalInfo['total'];
			if($total){
				$db->query("UPDATE ".$tb_area." SET `ishow`='".$ishow."' WHERE `infoid`='".$infoid."' AND 1");
				echo "<script>window.location.href='$back_url';</script>";
			}else{
				$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
			}

		}else{
			$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
		}
		break;

		//区域列表
	default:
		/**
		 * 二级区域页面跳转url传递参数
		 */


		$pid=empty($_GET['pid'])?"0":$_GET['pid'];

		$level=empty($_GET['level'])?"1":$_GET['level'];

		$fpage = $fun->setPageUri();//该页面;

		/**
		 * 用于返回到上级,标题显示
		*/
		$fid=$db->get_one("SELECT `parentid`,`title` FROM ".$tb_area." WHERE `infoid`='".$pid."' AND `is_valid`='1' AND 1");
		$smarty->assign("fid",$fid['parentid']);

		$info_cata_name=(empty($fid['title']))?"省市":$fid['title'];
		$smarty->assign("info_cata_name",$info_cata_name);


		$area_sql="SELECT `infoid`, `parentid`, `level`, `orderid`, `title`, `ishow`
					FROM ".$tb_area." WHERE `parentid`='".$pid."'  AND `is_valid`='1' AND 1
					ORDER BY `orderid` DESC,`infoid` ASC ";

			/*--分页开始--*/
			$numpage = $db->pagedeal($area_sql, $pagesize);//调用分页处理函数

			//----------------------分页相关的模板变量替换-------------------------//
			$smarty->assign("recordcount", $recordcount); 	//总记录数
			$smarty->assign("pagecount", $pagecount);  		//总页数
			$smarty->assign("prepage", $page - 1);     		//上一页
			$smarty->assign("nexpage", $page + 1);     		//下一页
			$smarty->assign("numpage", $numpage);    		//数字分页

			//SQL LIMIT起始
			$startloc=(($page-1)*$pagesize<0)?0:($page-1)*$pagesize;

			/*--分页结束--*/
			/**
			 * 共4条记录，当前第1/1页  首页  上页   下页  尾页
			*/
			$areaValue=$db->get_all($area_sql." LIMIT $startloc,$pagesize");

			$i=0;											//定义序号初始为0
			$area_arr=array();
			foreach ($areaValue as $key=>$value){

				//序号
				$i++;
				$pageid=(($page-1)*$pagesize)+$i;

				/*子类个数*/
				$subclass_num=$db->get_one("SELECT COUNT(`infoid`) AS total  FROM ".$tb_area." WHERE  `parentid`='".$value['infoid']."'  AND `is_valid`='1' AND 1");

				$area_arr[]=array("pageid"=>$pageid,
								"id"=>$value['infoid'],
								"subclassnum"=>$subclass_num['total'],
								"parentid"=>$value['parentid'],
								"level"	=>$value['level'],
								"orderid"=>$value['orderid'],
								"title"	=>$value['title'],
								"ishow"	=>$value['ishow']);

			}

	$smarty->assign("area_arr",$area_arr);			//区域信息


	$act_url=$_SESSION['act_url']=$fun->GetCurUrl(1);//获取URL的路径存入session


	/*批量删除部分*/
	if(!empty($_POST['delall']))					//用于判断复选框有无勾选以及是否点击删除
	{
		if(isset($_POST['del'])){
			$del_id=$_POST['del'];
			foreach ($del_id as $key=>$value){
				$total_s=$db->get_one("SELECT COUNT(`infoid`) AS total  FROM ".$tb_area." WHERE  `parentid`='".$value."' AND 1");
				if(!empty($total_s['total'])){
					$fun->msg("无法删除有子类的区域，请先将其子类删除","?pid=".$_GET['pid']."&level=".$_GET['level']."&page=".$_GET['page'],1);
					exit();
				}
			}

			$ids=implode("','",$_POST['del']);										//implode函数把数组元素组合成一个用","分开的字符串
			$del_info_cata=$db->delete($tb_area,"`infoid` in ('".$ids."')");		//删除tb_area表中对应的数据

			if($del_info_cata==true){
				$fun->msg("删除成功",$act_url,1);
				$db->close();
			}
			else{
				$fun->msg("删除失败",$act_url,1);
			}
		}else{
			$fun->msg("请选择要删除的区域",$act_url,1);
		}

	}


	/*批量更新排序*/
	if(!empty($_POST['replace'])){
		$all_id=isset($_POST['all'])?$_POST['all']:"";//所有id
		if(isset($all_id)){
			$order_id=isset($_POST['rep'])?$_POST['rep']:"";//更新的排序id
			foreach($all_id as $key=>$value){
				$db->query("UPDATE ".$tb_area." SET `orderid`='".$order_id[$key]."' WHERE `infoid`='".$value."' AND 1");
			}
			echo "<script>window.location.href='$act_url';</script>";
		}
	}
		$smarty->assign("pid",$pid);			//自身id
		$smarty->assign("level",$level);		//层级
		$smarty->assign("fpage", $fpage); 		//要分页的网页

		break;
}
$smarty->assign("page",$page);
$smarty->assign("title",$cfg['sysName']);
$smarty->display("area.tpl");//模版页
?>